package examples
{
	import com.wskeee.ec.components.Button;
	import com.wskeee.ec.components.CheckBox;
	import com.wskeee.ec.components.Label;
	import com.wskeee.ec.components.TextInput;
	import com.wskeee.ec.components.list.List;
	import com.wskeee.ec.containers.Container;
	import com.wskeee.ec.containers.GridBox;
	import com.wskeee.ec.containers.VBox;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;

	public class ListExample
	{
		public static function createList():Container
		{
			var mainBox:VBox = new VBox();
			var head:Label = new Label();
			head.text = "List";
			mainBox.verticalGap = 20;
			mainBox.paddingTop = mainBox.paddingBottom = mainBox.paddingLeft = mainBox.paddingRight = 5;
			mainBox.addChild(head);
			
			var list:List = new List();
			list.width = 150;
			list.addEventListener(Event.CHANGE,function(evt:Event):void
			{
				infoLabel.text = "selectedIndexs : "+list.selectedIndexs
				if(list.selectedItem)
					infoLabel.text+=" label: "+list.selectedItem.label;
			});
			function createListForNum(value:int):void
			{
				var ds:Vector.<Object> = new Vector.<Object>(value);
				for(var i:int=0;i<value;i++)
					ds[i] = {label:"wskeee"+i};	
				list.dataProvider = ds;
				list.selectedIndex = -1;
			}
			mainBox.addChild(list);
			var infoLabel:Label = new Label();
			infoLabel.text = "selectedIndexs : -1";
			mainBox.addChild(infoLabel);
			//---------------------
			//control
			//---------------------
			var controlBox:GridBox = new GridBox(3);
			controlBox.autoSize = true;
			var button:Button = new Button();
			button.label = "重新赋值list";
			button.addEventListener(MouseEvent.CLICK,function(evt:MouseEvent):void
			{
				createListForNum(int(listTxt.text));
			});
			var listTxt:TextInput = new TextInput("4");
			listTxt.toolTip = "输入需要创建list列表数目"
			listTxt.width = 20;
			createListForNum(1);
			
			controlBox.addChild(button);
			controlBox.addChild(listTxt);
			
			button = new Button();
			button.label = "add";
			button.addEventListener(MouseEvent.CLICK,function(evt:MouseEvent):void
			{
				list.addItem({label:"new"+Math.round(Math.random()*1000)});
			});
			controlBox.addChild(button);
			button = new Button();
			button.label = "remove";
			button.addEventListener(MouseEvent.CLICK,function(evt:MouseEvent):void
			{
				list.removeItemAt(0);
			});
			controlBox.addChild(button);
			button = new Button();
			button.label = "clear";
			button.addEventListener(MouseEvent.CLICK,function(evt:MouseEvent):void
			{
				list.removeAll();
			});
			controlBox.addChild(button);
			//多选
			var cb:CheckBox = new CheckBox();
			cb.label = "多选";
			cb.addEventListener(Event.CHANGE,function(evt:Event):void
			{
				list.allowMultipleSelection = CheckBox(evt.currentTarget).selected;
			});
			controlBox.addChild(cb);
			//autoHideScrollbar
			cb = new CheckBox();
			cb.label = "autoHideScrollbar";
			cb.addEventListener(Event.CHANGE,function(evt:Event):void
			{
				list.autoHideScrollbar = evt.target.selected;
			});
			controlBox.addChild(cb);
			mainBox.addChild(controlBox);
			
			return mainBox;
		}
	}
}